package com.wanmait.wisdom.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wisdom.entity.Admin;
import com.wanmait.wisdom.entity.AdminRoles;
import com.wanmait.wisdom.mapper.AdminMapper;
import com.wanmait.wisdom.manage.service.ManageAdminService;
import com.wanmait.wisdom.mapper.AdminRolesMapper;
import com.wanmait.wisdom.util.AjaxResult;
import com.wanmait.wisdom.util.JWTUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class ManageAdminServiceImpl extends ServiceImpl<AdminMapper,Admin> implements ManageAdminService
{

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private AdminRolesMapper adminRolesMapper;

        @Value("60")
        private int expireMinutes;


    @Override
    public AjaxResult Login(Admin admin) {

        if(admin==null){return null;}
        if(!StringUtils.hasText(admin.getAdminUsername())){return null;}
        if(!StringUtils.hasText(admin.getAdminPassword())){return null;}
        Admin loginAdmin= adminMapper.Login(admin);
        if(loginAdmin!=null){
            Map<String,String> claims = new HashMap<>();
            claims.put("adminusername",admin.getAdminUsername());
            String manageToken = JWTUtils.createToken(loginAdmin.getId()+"",expireMinutes,loginAdmin.getAdminPassword(),claims);
            loginAdmin.setAdminPassword(null);
            return AjaxResult.success("登陆成功").put("admin",loginAdmin).put("manageToken",manageToken);

        }

        return AjaxResult.error(10001,"账号或密码错误");
    }

    @Override
    public Admin findById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public AjaxResult findAll() {
        List<Admin> adminList = adminMapper.selectList(null);
        return AjaxResult.success("查询成功").put("admin",adminList);
    }

    @Override
    public AjaxResult AddadminChangepassword(Admin admin) {
        if(admin.getId()!=null){
//            LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
            adminMapper.updateById(admin);
//            saveOrUpdate(admin);
            return AjaxResult.success("修改成功");
        }
        else {
            admin.setState(true);
            save(admin);
            return AjaxResult.success("添加成功");
        }

    }

    @Override
    public AjaxResult listPage(Integer pageNum, Integer pageSize) {
        Page<Admin> page = new Page<>(pageNum,pageSize);
        Page<Admin> adminPage = adminMapper.selectPage(page,null);
        adminPage.getRecords().forEach(admin -> {admin.setAdminRoles(adminRolesMapper.selectById(admin.getRoleId()));});
        return AjaxResult.success("查询成功").put("adminPage",adminPage);

    }

    @Override
    public AjaxResult checkName(Admin admin) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(
                "admin_username",admin.getAdminUsername());
        Admin adminck= adminMapper.selectOne(queryWrapper);
        return AjaxResult.success("查询成功").put("admin",adminck) ;
    }

    @Override
    public AjaxResult getAdminid(String  id) {
        Admin admin = adminMapper.selectById(id);
        return AjaxResult.success("查询成功").put("admin",admin);
    }

    @Override
    public AjaxResult admindelete(Admin admin) {
        if(admin.getState()){
            admin.setState(false);
        }else{
            admin.setState(true);
        }
        adminMapper.updateByPrimaryKeySelective(admin) ;return AjaxResult.success("操作成功");
    }


}
